﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DIS_App_Client.UserManager;
using DIS_App_Client.Helpers;
using DIS_App_Client.MainManagers;

namespace DIS_App_Client
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            // Init controls
            InitControls();
        }

        private void InitControls()
        {
            menManageFunctions.Enabled = false;
            menChemistWareHouses.Enabled = false;
            menStaffs.Enabled = false;
            menItems.Enabled = false;
            menPromotionDiscount.Enabled = false;
            menRecordOrders.Enabled = false;
            menDailyReports.Enabled = false;
            menWeeklyReports.Enabled = false;
            menCategories.Enabled = false;

            ttbtnItems.Enabled = false;
            ttbtnPromotionDiscount.Enabled = false;
            ttbtnRecordOrders.Enabled = false;
            ttbtnDailyReports.Enabled = false;
            ttbtnWeeklyReports.Enabled = false;
            ttbtnChemistWareHouses.Enabled = false;
            ttbtnStaffs.Enabled = false;
        }

        private void menAbout_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            // Get connection string successfull, then open connection
            var lstEmployee = BusService.Instance.Employee_SelectAll();
            if (lstEmployee.Length > 0)
            {
                FrmLogin frm = new FrmLogin();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                menLogin.Enabled = false;
                menLogOut.Enabled = false;
                menChangePassword.Enabled = false;
                menManageFunctions.Enabled = true;
                menStaffs.Enabled = true;
            }
            // Init messages
            XMLHelper.GetAnnouncement(Application.StartupPath + @"\Configs\announcement.xml");
        }

        private void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVariables.g_CurrentUser = BusService.Instance.Employee_SelectByUserName(username);

            if (GlobalVariables.g_CurrentUser == null)
            {
                success = false;
                infoText = "UserName is not exist in system!";
            }
            else
            {
                GlobalVariables.g_CurrentUser = BusService.Instance.Employee_SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVariables.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Wrong password!";
                }
                else
                {
                    infoText = "Hello  " + GlobalVariables.g_CurrentUser.UserName + "  logged in to system!";
                }
            }

            if (!success)
            {
                MessageBox.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmLogin frm = new FrmLogin();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                menLogin.Enabled = false;
                menManageFunctions.Enabled = false;
                menChemistWareHouses.Enabled = false;
                menStaffs.Enabled = false;
                menItems.Enabled = false;
                menCategories.Enabled = false;
                menRecordOrders.Enabled = false;
                menDailyReports.Enabled = false;
                menWeeklyReports.Enabled = false;
                menPromotionDiscount.Enabled = false;
                
                ttbtnItems.Enabled = false;
                ttbtnPromotionDiscount.Enabled = false;
                ttbtnRecordOrders.Enabled = false;
                ttbtnDailyReports.Enabled = false;
                ttbtnWeeklyReports.Enabled = false;
                ttbtnChemistWareHouses.Enabled = false;
                ttbtnStaffs.Enabled = false;

                // Mapping name of controls with specifed enums
                var dictFucns = AllowFunctionsManager.InitFuncsMapping();
                // Store list of functionalities that user allow using
                var funcsAllowUsing = AllowFunctionsManager.InitFuncsAllowUsing();

                // Disable some functions that user not allow using
                foreach (var item in dictFucns)
                {
                    foreach (var item2 in funcsAllowUsing)
                    {
                        if (item.Value == item2)
                        {
                            AllowFunctionsManager.EnableFuncAllowUsing(pnlMain, item.Key);
                            break;
                        }
                    }
                }

                MessageBox.Show(infoText);
            }
        }

        private void menLogOut_Click(object sender, EventArgs e)
        {
            menLogin.Enabled = true;
            GlobalVariables.g_CurrentUser = null;

            FrmLogin frm = new FrmLogin();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void menChangePassword_Click(object sender, EventArgs e)
        {
            FrmChangePassword frm = new FrmChangePassword();
            frm.ShowDialog();
        }

        private void menManageFunctions_Click(object sender, EventArgs e)
        {
            FrmManageRoleFunctions frm = new FrmManageRoleFunctions();
            frm.ShowDialog();
        }

        private void menExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menCategories_Click(object sender, EventArgs e)
        {
            FrmManageCategory frm = new FrmManageCategory();
            frm.ShowDialog();
        }

        private void menStaffs_Click(object sender, EventArgs e)
        {
            FrmManageEmployee frm = new FrmManageEmployee();
            frm.ShowDialog();
        }

        private void menChemistWareHouses_Click(object sender, EventArgs e)
        {
            FrmManageChemistWareHouse frm = new FrmManageChemistWareHouse();
            frm.ShowDialog();
        }

        private void menItems_Click(object sender, EventArgs e)
        {
            FrmSelectCategory frm = new FrmSelectCategory();
            frm.Handler += ShowItemDlg;
            frm.ShowDialog();
        }

        private void ShowItemDlg(object sender, EventArgs e)
        {
            TransferSimpleDataEvent data = (TransferSimpleDataEvent)e;
            FrmManageItem frm = new FrmManageItem(data.Data);
            frm.ShowDialog();
        }

        private void menPromotionDiscount_Click(object sender, EventArgs e)
        {
            FrmSelectCategory frm = new FrmSelectCategory();
            frm.Handler += ShowPromotionDiscountDlg;
            frm.ShowDialog();
        }

        private void ShowPromotionDiscountDlg(object sender, EventArgs e)
        {
            TransferSimpleDataEvent data = (TransferSimpleDataEvent)e;
            FrmManagePromotion frm = new FrmManagePromotion(data.Data);
            frm.ShowDialog();
        }

        private void menRecordOrders_Click(object sender, EventArgs e)
        {
            FrmManageOrders frm = new FrmManageOrders();
            frm.ShowDialog();
        }

        private void menDailyReports_Click(object sender, EventArgs e)
        {
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                FrmViewDailyReport frm = new FrmViewDailyReport();
                frm.ShowDialog();
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                FrmManageDailyReport frm = new FrmManageDailyReport();
                frm.ShowDialog();
            }
        }

        private void menWeeklyReports_Click(object sender, EventArgs e)
        {
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                FrmViewWeeklyReport frm = new FrmViewWeeklyReport();
                frm.ShowDialog();
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                FrmManageWeeklyReport frm = new FrmManageWeeklyReport();
                frm.ShowDialog();
            }
        }

        private void ttbtnItems_Click(object sender, EventArgs e)
        {
            FrmSelectCategory frm = new FrmSelectCategory();
            frm.Handler += ShowItemDlg;
            frm.ShowDialog();
        }

        private void ttbtnPromotionDiscount_Click(object sender, EventArgs e)
        {
            FrmSelectCategory frm = new FrmSelectCategory();
            frm.Handler += ShowPromotionDiscountDlg;
            frm.ShowDialog();
        }

        private void ttbtnRecordOrders_Click(object sender, EventArgs e)
        {
            FrmManageOrders frm = new FrmManageOrders();
            frm.ShowDialog();
        }

        private void ttbtnDailyReports_Click(object sender, EventArgs e)
        {
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                FrmViewDailyReport frm = new FrmViewDailyReport();
                frm.ShowDialog();
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                FrmManageDailyReport frm = new FrmManageDailyReport();
                frm.ShowDialog();
            }
        }

        private void ttbtnWeeklyReports_Click(object sender, EventArgs e)
        {
            if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.NATIONAL)
            {
                FrmViewWeeklyReport frm = new FrmViewWeeklyReport();
                frm.ShowDialog();
            }
            else if (CommonHelper.GetRole(GlobalVariables.g_CurrentUser) == ROLE.LOCAL)
            {
                FrmManageWeeklyReport frm = new FrmManageWeeklyReport();
                frm.ShowDialog();
            }
        }

        private void ttbtnChemistWareHouses_Click(object sender, EventArgs e)
        {
            FrmManageChemistWareHouse frm = new FrmManageChemistWareHouse();
            frm.ShowDialog();
        }

        private void ttbtnStaffs_Click(object sender, EventArgs e)
        {
            FrmManageEmployee frm = new FrmManageEmployee();
            frm.ShowDialog();
        }
    }
}
